package com.yh.csx.bsf.ods.utils;

import java.util.ArrayList;
import java.util.List;

import com.alibaba.otter.canal.protocol.CanalEntry.Column;
import com.yh.csx.bsf.ods.meta.OdsColumn;
import com.yh.csx.bsf.ods.meta.OdsColumn.OdsColumnBuilder;

public class OdsTypeUtils {
	public static List<OdsColumn> convert2OdsColumn(List<Column> columns) {
		List<OdsColumn> ret = new ArrayList<OdsColumn>();
		if (columns != null) {
			columns.forEach(item -> {				
				String mydqlType=item.getMysqlType();
				if(mydqlType.contains("(")) {
					mydqlType=mydqlType.substring(0,mydqlType.indexOf("("));
				}	
				
				if("JSON".equalsIgnoreCase(item.getMysqlType())){
					mydqlType="LONGTEXT";
				}
				ret.add(OdsColumnBuilder.build().setColumnName(item.getName()).setDataType(	mydqlType)
						.setColumnType(item.getMysqlType().toLowerCase()).
						setNullable(item.getIsNull()).setNumericPrecision(32).setNumericScale(10).setColumnKey(item.getIsKey()?"PRI":"").
						setValue(item.getValue()));
			});			
		}
		return ret;
	}
	
	public static String getDataType(String colmenType) {
		String mydqlType=colmenType;
		if(mydqlType.contains("(")) {
			mydqlType=mydqlType.substring(0,mydqlType.indexOf("("));
		}	
		
		if("JSON".equalsIgnoreCase(colmenType)){
			mydqlType="LONGTEXT";
		}
		return mydqlType;
	}
}
